
package com.wiesloch.controller;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.wiesloch.po.Goods;
import com.wiesloch.po.GoodsVo;
import com.wiesloch.service.GoodsService;

/**
 * <p>ClassName:GoodsCobtroller</p>
 * <p>Description:商品功能的Controller</p>
 * <p> E-mail:jintao_iknow@163.com </p>
 * Date:     2016年5月14日 下午6:33:06 <br/>
 * @author   tao95
 * @version  1.0
 * @since    JDK 1.7
 *
 */
@Controller
@RequestMapping("goodsController")
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	
		/**
		 * @description 显示所有的系列页面
		 * @param request
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/allSeriesUI",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView allSeriesUI(HttpServletRequest request) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			
			List<Goods> resultList = goodsService.selectAllSeries();
			
			List<GoodsVo>seriesList = new ArrayList<GoodsVo>();
			for (Goods good : resultList) {
				
				String pictureUrl = good.getGoods_pictureUrl();
				String [] pics = pictureUrl.split(";");
				GoodsVo goodsVo = new GoodsVo();
				goodsVo.setGoods(good);
				goodsVo.setPictureUrl(pics[0]);
				seriesList.add(goodsVo);
				System.out.println();
			}
			modelAndView.addObject("seriesList", seriesList);
			modelAndView.setViewName("goods/allSeries");
			return modelAndView;
			
		}
		
		/**
		 * @description 显示一个系列下的所有的商品信息
		 * @param request
		 * @param goods_pid
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/allGoodsUI",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView allGoodsUI(HttpServletRequest request,Integer goods_pid) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			List<Goods> resultList = goodsService.selectAllGoodsByPid(goods_pid);
			List<GoodsVo>goodsList = new ArrayList<GoodsVo>();
			for (Goods good : resultList) {
				String pictureUrl = good.getGoods_pictureUrl();
				String [] pics = pictureUrl.split(";");
				GoodsVo goodsVo = new GoodsVo();
				goodsVo.setGoods(good);
				goodsVo.setPictureUrl(pics[0]);
				goodsList.add(goodsVo);
				System.out.println();
			}
			modelAndView.addObject("goods",goodsList.get(0));
			modelAndView.addObject("goodsList", goodsList);
			modelAndView.setViewName("goods/allGoods");
			return modelAndView;
			
		}
		
		/**
		 * @description 添加一个系列
		 * @param request
		 * @param response
		 * @param goods
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/saveSeries",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView saveSeries(HttpServletRequest request,HttpServletResponse response, Goods goods) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
			ServletContext servletContext = request.getServletContext();
   			String Contextpath = servletContext.getRealPath("/");
   			Contextpath = Contextpath.substring(0, Contextpath.lastIndexOf("\\")-1);
   			Contextpath = Contextpath.substring(0, Contextpath.lastIndexOf("\\"));
   			String goods_pictureUrl = "";
			//判断 request 是否有文件上传,即多部分请求  
	        if(multipartResolver.isMultipart(request)){  
	            //转换成多部分request    
	            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
	            //取得request中的所有文件名  
	            Iterator<String> iter = multiRequest.getFileNames();  
	           
	           while(iter.hasNext()){  
	        	
	        	MultipartFile file = multiRequest.getFile(iter.next());
	        	if(file != null){  
	                    //取得当前上传文件的文件名称  
	                    String myFileName = file.getOriginalFilename();  
	                    //如果名称不为"",说明该文件存在，否则说明该文件不存在  
	                    if(myFileName.trim() !=""){  
	                        //重命名上传后的文件名  
	                        String fileName = file.getOriginalFilename();
	                        goods_pictureUrl += fileName+=";";
	                        //定义上传路径  
	                        String path = Contextpath+ "/webimages/" + fileName;
	                        File localFile = new File(path);
	                        file.transferTo(localFile);
	                    }  
	                }  
	              
	            } 
	            goods_pictureUrl = goods_pictureUrl.substring(0,goods_pictureUrl.length()-1);
	        }  
	        goods.setGoods_pid(0);
	        goods.setGoods_pictureUrl(goods_pictureUrl);
	        goodsService.insertSeries(goods);
	        System.out.println(goods);
	        modelAndView = allSeriesUI(request);
			return modelAndView;
		}
		
		/**
		 * @description 添加一个商品
		 * @param request
		 * @param response
		 * @param goods
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/saveGoods",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView saveGoods(HttpServletRequest request,HttpServletResponse response, Goods goods,Integer pid) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
			ServletContext servletContext = request.getServletContext();
   			String Contextpath = servletContext.getRealPath("/");
   			Contextpath = Contextpath.substring(0, Contextpath.lastIndexOf("\\")-1);
   			Contextpath = Contextpath.substring(0, Contextpath.lastIndexOf("\\"));
   			String goods_pictureUrl = "";
			//判断 request 是否有文件上传,即多部分请求  
	        if(multipartResolver.isMultipart(request)){  
	            //转换成多部分request    
	            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
	            //取得request中的所有文件名  
	            Iterator<String> iter = multiRequest.getFileNames();  
	           
	           while(iter.hasNext()){  
	        	
	        	MultipartFile file = multiRequest.getFile(iter.next());
	        	if(file != null){  
	                    //取得当前上传文件的文件名称  
	                    String myFileName = file.getOriginalFilename();  
	                    
	                    //如果名称不为"",说明该文件存在，否则说明该文件不存在  
	                   if(myFileName.trim() !=""){  
	                        //重命名上传后的文件名  
	                        String fileName = file.getOriginalFilename();
	                        goods_pictureUrl = goods_pictureUrl+ fileName+";";
	                        //定义上传路径  
	                        String path = Contextpath+ "/webimages/" + fileName;
	                        File localFile = new File(path);
	                        file.transferTo(localFile);
	                    }  
	                }  
	              
	            } 
	            goods_pictureUrl = goods_pictureUrl.substring(0,goods_pictureUrl.length()-1);
	        }  
	        goods.setGoods_pid(pid);
	        goods.setGoods_pictureUrl(goods_pictureUrl);
	        goodsService.insertGoods(goods);
	        System.out.println(goods);
	        modelAndView = allGoodsUI(request,pid);
			return modelAndView;
		}
		
		/**
		 * @description 删除一个系列
		 * @param request
		 * @param goods_id
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/deleteSeries",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView deleteSeries(HttpServletRequest request,Integer goods_id) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			goodsService.deleteGoodsById(goods_id);
			modelAndView = allSeriesUI(request);
			return modelAndView;
			
		}
		
	    /**
	     * @description 更新系列的状态(删除,冻结,正常)
	     * @param request
	     * @param goods
	     * @return
	     * @throws Exception
	     */
		@RequestMapping(value="/updateSeriesStateById",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView updateSeriesStateById(HttpServletRequest request,Goods goods) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			System.out.println(goods);
			goodsService.updateGoodsStateById(goods);
			modelAndView = allSeriesUI(request);
			return modelAndView;
			
		}
		
		/**
		 * @description 删除一个商品
		 * @param request
		 * @param goods_id
		 * @param goods_pid
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/deleteGoods",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView deleteGoods(HttpServletRequest request,Integer goods_id,Integer goods_pid) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			goodsService.deleteGoodsById(goods_id);
			modelAndView = allGoodsUI(request,goods_pid);
			return modelAndView;
			
		}
		
		/**
		 * @description 更新商品的状态(删除,冻结,正常)
		 * @param request
		 * @param goods
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/updateGoodsStateById",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView updateGoodsStateById(HttpServletRequest request,Goods goods) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			System.out.println(goods);
			goodsService.updateGoodsStateById(goods);
			modelAndView = allGoodsUI(request,goods.getGoods_pid());
			return modelAndView;
		}
		
		
		/**
		 * @description 添加系列的UI界面
		 * @param request
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/saveSeriesUI",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView saveSeriesUI(HttpServletRequest request) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			
			modelAndView.setViewName("goods/saveSeriesUI");
			return modelAndView;
		}
		
		
	
		/**
		 * 
		 * @description 修改系列的UI界面
		 * @param request
		 * @param goods_id
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/editSeriesUI",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView editSeriesUI(HttpServletRequest request,Integer goods_id) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			Goods goods = goodsService.selectGoodsById(goods_id);
			System.out.println(goods);
			modelAndView.addObject("goods", goods);
			modelAndView.setViewName("goods/saveSeriesUI");
			return modelAndView;
		}
		
		
		/**
		 * 
		 * @description 修改系列
		 * @param request
		 * @param goods
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/editSeries",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView editSeries(HttpServletRequest request,Goods goods) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			goodsService.updateSeries(goods);
			modelAndView = allSeriesUI(request);
			return modelAndView;
		}
		
		
		/**
		 * @description 添加商品的UI界面
		 * @param request
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/saveGoodsUI",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView saveGoodsUI(HttpServletRequest request,Integer pid) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.addObject("pid", pid);
			modelAndView.setViewName("goods/saveGoodsUI");
			return modelAndView;
		}
		

		/**
		 * @description 添加商品的UI界面
		 * @param request
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/editGoodsUI",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView editGoodsUI(HttpServletRequest request,Integer pid,Integer goods_id) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.addObject("pid", pid);
			Goods goods = goodsService.selectGoodsById(goods_id);
			modelAndView.addObject("goods", goods);
			modelAndView.setViewName("goods/saveGoodsUI");
			return modelAndView;
		}
		
		/**
		 * @description 添加商品的UI界面
		 * @param request
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value="/editGoods",method = { RequestMethod.POST, RequestMethod.GET })
		public ModelAndView editGoods(HttpServletRequest request,Goods goods) throws Exception{
			ModelAndView modelAndView = new ModelAndView();
			System.out.println(goods);
			goodsService.updateGoods(goods);
			
			modelAndView = allGoodsUI(request,goods.getGoods_pid());
			return modelAndView;
		}
		
}
